package com.dragon.service.impl;

import cn.hutool.core.io.FileUtil;
import com.dragon.pojo.CodePayload;
import com.dragon.pojo.ExecuteCodeResponse;
import com.dragon.pojo.ExecuteMessage;
import com.dragon.pojo.JudgeInfo;
import com.dragon.pojo.enums.JudgeInfoMessageEnum;
import com.dragon.service.SandboxService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public abstract class CommonServiceImpl implements SandboxService {

    @Override
    public ExecuteCodeResponse getOutputResponse(List<ExecuteMessage> executeMessageList, CodePayload payload, File codeFile) {
        // 创建测试结果列表
        List<JudgeInfo> judgeInfoList = new ArrayList<>();
        int testTotal = executeMessageList.size();
        int successTotal = 0;
        for (int i = 0; i < testTotal; i++) {
            ExecuteMessage executeMessage = executeMessageList.get(i);
            // 默认ac
            int status = 1;
            // 检查是否正常运行
            if(executeMessage.getExitValue() == 1 && !Objects.equals(executeMessage.getErrorMessage(), "Time Limit Exceeded")){
                status = 7;
            }else {
                // 核对输出结果
                if(!Objects.equals(executeMessage.getMessage(), payload.getOutputList().get(i))){
                    status = 2;
                    // 检查是否输出格式错误
                    if(Objects.equals(executeMessage.getMessage().trim(),payload.getOutputList().get(i))){
                        status = 6;
                    }
                }
                // 检查是否输出超限
                if(executeMessage.getMessage().length()>100000){
                    status = 8;
                }
                // 检查内存是否超限
                if(executeMessage.getMemory() != null){
                    if(executeMessage.getMemory()>payload.getMemoryLimit()){
                        status = 4;
                    }
                }
                // 检查时间是否超限
                if(executeMessage.getTime()>payload.getTimeLimit()){
                    status = 5;
                }

            }
            if(status == 1){
                successTotal ++;
            }
            judgeInfoList.add(new JudgeInfo(JudgeInfoMessageEnum.getInfoByValue(status),executeMessage.getTime(),executeMessage.getMemory()));
        }
        return ExecuteCodeResponse.success(judgeInfoList, testTotal, successTotal, getFileSize(codeFile), payload.getCode());
    }

    @Override
    public void deleteFile(File userCodeFile) {
        if(userCodeFile.getParentFile() != null){
            // 获取用户代码所在父目录的绝对路径
            String userCodeParentPath = userCodeFile.getParentFile().getAbsolutePath();

            boolean del = FileUtil.del(userCodeParentPath);
            log.info("运行已完成，删除:{}",del ? "成功" : "失败");

        }
        // 如果用户代码文件没有父目录，则默认认为删除成功
    }

    @Override
    public long getFileSize(File codeFile) {
        return FileUtil.size(codeFile);
    }
//
//    @Override
//    public String getSourceCode(File codeFile) {
//        return FileUtil.readUtf8String(codeFile);
//    }
}